package jwdt.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Map;
import java.util.Properties;

import jwdt.common.Constant;
import jwdt.vo.DevToolsVo;
import jwdt.vo.InitProDataSourceVo;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class JWDTSettingUtils {

	private static final String SETTING_PATH = "/.settings";
	private static final String SETTING_INITPROFRAMEWORK_FILE = "/.jwdt.initproframework";
	private static final String SETTING_DEVTOOLS_FILE = "/.jwdt.devtools";
	private static final String SETTING_ENCODING_FILE = "/org.eclipse.core.resources.prefs";
	
	public static void saveSettings(String projectPath,
			Map<String, String> fws,
			Map<String, InitProDataSourceVo> initProDataSourceVoMap) throws IOException {
		// create Document
		Document document = DocumentHelper.createDocument();
		// root jwdt
		Element jwdt = document.addElement("jwdt");
		// framework
		Element framework = jwdt.addElement("framework");		
		framework.addAttribute("mvc", fws.get(Constant.MVC));
		framework.addAttribute("ioc", fws.get(Constant.IOC));
		framework.addAttribute("orm", fws.get(Constant.ORM));
		framework.addAttribute("page", fws.get(Constant.PAGE));
		// framework
		for(String key : initProDataSourceVoMap.keySet()){
			InitProDataSourceVo initProDataSourceVo = initProDataSourceVoMap.get(key);
			Element datasource = jwdt.addElement("datasource");
			datasource.addAttribute("name", initProDataSourceVo.getDataSourceName());
			datasource.addAttribute("database", initProDataSourceVo.getDataBaseType());
			datasource.addAttribute("type", initProDataSourceVo.getDataSourceType());
			if(Constant.DATASOURCE_TYPE_JDBC.equals(initProDataSourceVo.getDataSourceType())){				
				datasource.addAttribute("driver", initProDataSourceVo.getDriverClass());				
				datasource.addAttribute("url", initProDataSourceVo.getDataBaseUrl());
				datasource.addAttribute("username", initProDataSourceVo.getUserName());
				datasource.addAttribute("password", initProDataSourceVo.getPassWord());
			}
			if(Constant.DATASOURCE_TYPE_JNDI.equals(initProDataSourceVo.getDataSourceType())){				
				datasource.addAttribute("jndi", initProDataSourceVo.getJndiName());				
			}	
			datasource.addAttribute("isdefault", String.valueOf(initProDataSourceVo.isDefault()));
		}
		
		// create file
		File file = new File(projectPath+SETTING_PATH+SETTING_INITPROFRAMEWORK_FILE);
		if (!file.exists()) {
			file.createNewFile();
		}
		Writer fileWriter = new FileWriter(file);
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setNewLineAfterDeclaration(false);
		XMLWriter xmlWriter = new XMLWriter(fileWriter, format);
		xmlWriter.write(document);
		xmlWriter.close();
	}
	
	public static void saveSettings(String projectPath, DevToolsVo devToolsVo) throws IOException {
		File file = new File(projectPath+SETTING_PATH+SETTING_DEVTOOLS_FILE);
		if (!file.exists()) {
			file.createNewFile();
		}
		
		Properties prop = new Properties();
		InputStream fis = new FileInputStream(file);
		prop.load(fis);
		prop.clear();
		
		InitProDataSourceVo initProDataSourceVo = devToolsVo.getInitProDataSourceVo();
		prop.setProperty("name", initProDataSourceVo.getDataSourceName());
		prop.setProperty("database", initProDataSourceVo.getDataBaseType());
		prop.setProperty("type", initProDataSourceVo.getDataSourceType());		
		if(Constant.DATASOURCE_TYPE_JDBC.equals(initProDataSourceVo.getDataSourceType())){	
			prop.setProperty("driver", initProDataSourceVo.getDriverClass());
			prop.setProperty("url", initProDataSourceVo.getDataBaseUrl());			
			prop.setProperty("username", initProDataSourceVo.getUserName());	
			prop.setProperty("password", initProDataSourceVo.getPassWord());
		}
		if(Constant.DATASOURCE_TYPE_JNDI.equals(initProDataSourceVo.getDataSourceType())){	
			prop.setProperty("jndi", initProDataSourceVo.getJndiName());			
		}	
		prop.setProperty("isdefault", String.valueOf(initProDataSourceVo.isDefault()));	
		prop.setProperty("javasources", devToolsVo.getJavaSourcesPath());	
		prop.setProperty("package", devToolsVo.getPackageText());	
		prop.setProperty("tablepattern", devToolsVo.getTablePattern());	
		
		OutputStream fos = new FileOutputStream(file);
		prop.store(fos, null);
	}
	
	public static void saveEncodingSettings(String projectPath) throws IOException {
		File file = new File(projectPath+SETTING_PATH+SETTING_ENCODING_FILE);
		if (!file.exists()) {
			file.createNewFile();
		}
		
		Properties prop = new Properties();
		InputStream fis = new FileInputStream(file);
		prop.load(fis);
		prop.clear();
		
		prop.setProperty("eclipse.preferences.version", "1");
		prop.setProperty("encoding/<project>", "UTF-8");	
		
		OutputStream fos = new FileOutputStream(file);
		prop.store(fos, null);
	}
}
